package com.trgzs.bate.common;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @Author: dongl
 * @Email: dong980514280@gmail.com
 * @Create: 2025-03-05 16:06:01
 * @PS
 **/
@Component("au")
public class AuthUtils {

    public boolean check(String role) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication.getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .noneMatch(role::equals)) {
            throw new AccessDeniedException("User does not have the required permission");
        }
        return true;
    }
}
